﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MS.Core.Model;
using ReportDesignerServer.Common;
using ReportDesignerServer.DataModel;
using ReportDesignerServer.DataModels;
using ReportDesignerServer.Services;
using SqlSugar;
using System.Data;

namespace ReportDesignerServer.Controllers
{

    /// <summary>
    /// 护士工作量
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class NurseDayWorkController : ControllerBase
    {

        NurseDayWorkService nurseDayWorkService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nurseDayWorkService"></param>
        public NurseDayWorkController(NurseDayWorkService nurseDayWorkService)
        {
            this.nurseDayWorkService = nurseDayWorkService;
        }

        /// <summary>
        /// 通过月份组查询数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Rt GetNurseDayWorksByMouth(string mouth)
        {
            return nurseDayWorkService.GetNurseDayWorksByMouth(mouth);
        }

        /// <summary>
        /// 通过id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Rt GetNurseDayWorkById(string id)
        {
            return nurseDayWorkService.GetNurseDayWorkById(id);
        }


        /// <summary>
        /// 通过月份组查询数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt SaveNurseDayWork([FromBody] NurseDayWork model)
        {
            return nurseDayWorkService.SaveNurseDayWork(model);
        }

        /// <summary>
        /// 获取列配置字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Rt GetDictCol()
        {
            return Rt.SuccessObj(nurseDayWorkService.GetDictCol());
        }

        /// <summary>
        /// 获取列配置字典
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Rt SaveDictColSort([FromBody] List<ColShow> list)
        {
            int res = nurseDayWorkService.SaveDictColSort(list);
            if (res > 0)
                return Rt.Success();
            else return Rt.Fail();
        }

        /// <summary>
        /// 获取统计报表
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetExcel(string year)
        {
            DataTable dt = nurseDayWorkService.LoadData(year);
            var bytes = ExcelHelper.ExportArray(dt, string.Concat(new string[] { "老年护理工作量日报表统计( " + year + ")" }));
            // 返回文件
            return File(bytes, "application/vnd.ms-excel", year+"年统计报表");
        }
    }
}
